﻿using DoNetDrive.Core.Command;
using DoNetDrive.Core.Connector;
using DotNetty.Buffers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _8280WEBSocket.Command
{
    public class TCPIOObserverHandler : INRequestHandle
    {
        public delegate void DisposeEventHandler(INConnector connector, int len, string message);
        public delegate void OnRequestLogEventHandler(INConnector connector,  string message);
        Encoding UTF8 = Encoding.UTF8;
        public event DisposeEventHandler DisposeResponseEvent;
        public event DisposeEventHandler DisposeRequestEvent;
        public event OnRequestLogEventHandler OnRequestLog;
        public void Dispose()
        {
           
        }

        public void DisposeRequest(INConnector connector, IByteBuffer msg)
        {
            var iLen = msg.ReadableBytes;
            var sHex = msg.ReadString(iLen, UTF8);
            if (sHex.StartsWith("{\"msgtype\":", StringComparison.OrdinalIgnoreCase))
            {
                DisposeRequestEvent?.Invoke(connector, iLen, sHex);
            }
        }

        public void DisposeResponse(INConnector connector, IByteBuffer msg)
        {
            var iLen = msg.ReadableBytes;
            var sHex = msg.ReadString(iLen, UTF8);
            DisposeResponseEvent?.Invoke(connector, iLen, sHex);
        }
    }
}
